package com.auoem.radio.common.custview.wheel;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.auctus.am.framework.picker.IIntPickDataWrap;
import com.auoem.radio.R;
import com.auoem.radio.common.custview.wheel.adapter.ControlWheelAdapter;


public class SimplePickerDialog extends Dialog implements OnWheelChangedListener {

    private final static int DEFAULT_ITEMS = 5;

    public final static int INVALID = 0XFFFFFFFE;

    public final static int TEXT_DEFAULT_COLOR = 0xCCCCCC;
    public final static int TEXT_SELECT_COLOR = 0xCCCCCC;

    private Activity mContext;

//    OnChannelPickedListener mListener;

//    ControlPickData mControlPickData;

    OnPickListener mOnPickListener;
    OnWheelListener mOnWheelListener;

    IIntPickDataWrap mPickData;

    ControlWheelAdapter mPickDataAdapter;

    WheelView wheel;

    int index;
    int value;

    public SimplePickerDialog(Activity context, IIntPickDataWrap pickData1, int v1,
                              OnPickListener onPickListener, OnWheelListener onWheelListener) {
        super(context);
        mContext = context;
        mOnPickListener = onPickListener;
        mOnWheelListener = onWheelListener;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#00000000")));
        getWindow().setWindowAnimations(R.style.AnimBottom);
        View rootView = getLayoutInflater().inflate(
                R.layout.am_custview_wheel_dialog_simple_picker, null);
        int screenWidth = mContext.getWindowManager().getDefaultDisplay()
                .getWidth();
        LayoutParams params = new LayoutParams(screenWidth,
                LayoutParams.MATCH_PARENT);
        super.setContentView(rootView, params);

        initViews();
        update(pickData1, v1);
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        switch (wheel.getId()) {
            case R.id.wheel1:
                if (newValue != oldValue) {
                    index = newValue;
                    int v1 = mPickData.getValue(index);
                    if (mOnWheelListener != null) {
                        mOnWheelListener.onWheelPick(v1, index);
                    }
//                    mListener.onWheelPick(v1, index);
                }
                break;
        }
    }

    public interface OnChannelPickedListener {
        void onPicked(int sel1, int v1);

        void onWheelPick(int value, int index);
    }

    private void initViews() {
        wheel = findViewById(R.id.wheel1);
        wheel.setCyclic(false);
        wheel.setVisibleItems(DEFAULT_ITEMS);
        OnWheelClickedListener clickListener = new OnWheelClickedListener() {

            @Override
            public void onItemClicked(WheelView wheel, int itemIndex) {
                if (itemIndex != wheel.getCurrentItem()) {
                    wheel.setCurrentItem(itemIndex, true, 500);
                }
            }
        };
        wheel.addClickingListener(clickListener);
        wheel.setChangingListener(this);

        View done = findViewById(R.id.done);
        done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnPickListener != null) {
                    mOnPickListener.onPicked(index, mPickData.getValue(index));
                }
                dismiss();
            }
        });

        View cancel = findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void update(IIntPickDataWrap pickData1, int v1) {
        mPickData = pickData1;
        if (mPickDataAdapter == null) {
            mPickDataAdapter = new ControlWheelAdapter(mContext, mPickData);
        } else {
            mPickDataAdapter.update(mPickData, 0);
        }
        wheel.setViewAdapter(mPickDataAdapter);
        wheel.setCyclic(false);
        wheel.setVisibleItems(DEFAULT_ITEMS);
        if (v1 == INVALID) {
            wheel.setCurrentItem(0);
            return;
        }
        int index1 = mPickData.getIndexOnValue(v1);
        if (index1 >= 0 && index1 < mPickData.getSize()) {
            wheel.setCurrentItem(index1);
        } else {
            wheel.setCurrentItem(0);
        }
    }

    public void update(IIntPickDataWrap pickData1, int v1, OnPickListener onPickListener, OnWheelListener onWheelListener) {
        mPickData = pickData1;
        mOnPickListener = onPickListener;
        mOnWheelListener = onWheelListener;
        if (mPickDataAdapter == null) {
            mPickDataAdapter = new ControlWheelAdapter(mContext, mPickData);
        } else {
            mPickDataAdapter.update(mPickData, 0);
        }
        wheel.setViewAdapter(mPickDataAdapter);
        wheel.setCyclic(false);
        wheel.setVisibleItems(DEFAULT_ITEMS);
        if (v1 == INVALID) {
            wheel.setCurrentItem(0);
            return;
        }
        int index1 = mPickData.getIndexOnValue(v1);
        if (index1 >= 0 && index1 < mPickData.getSize()) {
            wheel.setCurrentItem(index1);
        } else {
            wheel.setCurrentItem(0);
        }
    }

    public interface OnPickListener {
        void onPicked(int sel1, int v1);
    }

    public interface OnWheelListener {
        void onWheelPick(int value, int index);
    }

}
